#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

int n, m;
int head[N];
int ver[N], Next[N], tot;
void add(int u, int v) {
  ++tot;
  ver[tot] = v;
  Next[tot] = head[u];
  head[u] = tot;
}

int dfn[N], low[N], num;
int scc[N], cnt, owner[N];
int st[N], top;

void tarjan(int u) {
  dfn[u] = low[u] = ++num;
  st[++top] = u;
  for (int i = head[u]; i; i = Next[i]) {
    int v = ver[i];
    if (!dfn[v]) {
      tarjan(v);
      low[u] = min(low[u], low[v]);
    } else if (!scc[v]) {
      low[u] = min(low[u], dfn[v]);
    }
  }
  if (dfn[u] == low[u]) {
    ++cnt;
    owner[cnt] = u;
    do {
      scc[st[top]] = cnt;
    } while (st[top--] != u);
  }
}

set<int> ch[N];
int ind[N];
void dfs(int u, vector<int>& res) {
  if (ch[u].empty()) {
    res.push_back(u);
    return;
  }
  for (int v : ch[u]) dfs(v, res);
}
void solve() {
  cin >> n;
  rep(i, 1, n) {
    int x;
    cin >> x;
    add(x, i);
  }
  rep(i, 1, n) if (!dfn[i]) tarjan(i);
  // rebuild
  rep(u, 1, n) {
    for (int i = head[u]; i; i = Next[i]) {
      int v = ver[i];
      if (scc[u] != scc[v]) {
        ch[scc[u]].insert(scc[v]);
        ind[scc[v]]++;
      }
    }
  }
  vector<pii> ans;
  vector<pii> a;
  vector<int> leaf;
  rep(i, 1, cnt) {
    if (!ind[i]) {
      leaf.clear();
      dfs(i, leaf);
      a.push_back(pii(i, leaf.back()));
      leaf.pop_back();
      for (int j : leaf) ans.push_back(pii(i, j));
    }
  }
  for (int i = 0; i < a.size(); i++) {
    int j = i + 1;
    if (j >= a.size()) j = 0;
    ans.push_back(pii(a[j].first, a[i].second));
  }
  if (ans.size() == 1 && ans[0].first == ans[0].second) {
    cout << 0 << endl;
    return;
  }
  cout << ans.size() << endl;
  for (pii x : ans) cout << owner[x.first] << ' ' << owner[x.second] << endl;
  return;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t = 1;
  while (t--) solve();
  return 0;
}